//
//  ByPersonalCenterVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/11/30.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByPersonalCenterVC.h"
#import "ByLoginVC.h"
#import "ByMyInformationVC.h"
#import "ByFeedbackVC.h"


@interface ByPersonalCenterVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) ZKBombView *popView;

@end

@implementation ByPersonalCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的";
    [self initViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    if (self.nameLabel) {
        self.nameLabel.text = BY_USER.nickname;
    }
}


- (ZKBombView *)popView{
    if (_popView == nil) {
        _popView = [[ZKBombView alloc] init];
        _popView.positionMode = ZKBombPositionModeBottom;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.masksToBounds = YES;

        
        UIButton *wechat1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [view addSubview:wechat1];
        [wechat1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view).offset(-50);
            make.top.equalTo(view).offset(40);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        [wechat1 setImage:[UIImage imageNamed:@"朋友圈"] forState:UIControlStateNormal];
        wechat1.tag = 100;
        
        UIButton *wechat2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [view addSubview:wechat2];
        [wechat2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view).offset(50);
            make.top.equalTo(view).offset(40);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        [wechat2 setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
        wechat2.tag = 101;
        
        [wechat1 addTarget:self action:@selector(wechatAction:) forControlEvents:UIControlEventTouchUpInside];
        [wechat2 addTarget:self action:@selector(wechatAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        _popView.contentView = view;
        
    }
    return _popView;
}

- (void)wechatAction:(UIButton *)btn{
    
    if (btn.tag == 100) {
        //[self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
    }else{
        //[self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
    }
    
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    [self.popView remove];
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    NSString* webpageUrl =  @"http://web1.jinpu.8raw.com/help/index";
    NSString *shareStr = @"建工宝分享";
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"建工宝分享"];
    id thumImage = imageView.image;
    
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:shareStr descr:@"水族之家" thumImage:thumImage];
    //设置网页地址
    shareObject.webpageUrl = webpageUrl;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        
        if (error) {
            [ByCoreToolAlert showFailureWithText:@"分享失败"];
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            [ByCoreToolAlert showSuccessWithText:@"分享成功"];
            
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

- (void)initViews{
    
    CGFloat height = 0;
    if (Device_Is_iPhoneX) {
        height = 180 + 88 - 64;
    }else{
        height = 180;
    }
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    headView.backgroundColor = BY_THEME_COLOR;
    [self.view addSubview:headView];
    headView.tag = 100;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAciton:)];
    [headView addGestureRecognizer:tap];
    
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [headView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).offset(15);
        make.bottom.equalTo(headView).offset(-30);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    [imageView setImageWithURL:[NSURL URLWithString:BOYE_AVATAR_URL(BY_USER.uid)] placeholderImage:DEFAULT_IMAGE completed:nil];
    imageView.layer.cornerRadius = 40;
    imageView.userInteractionEnabled = YES;
    imageView.layer.masksToBounds = YES;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [headView addSubview:nameLabel];
    [nameLabel  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(20);
        make.bottom.equalTo(imageView.mas_centerY).offset(0);
        make.right.equalTo(headView).offset(-10);
        make.height.mas_equalTo(@20);
    }];
    nameLabel.text = @"用户名";
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = FONT(16);
    nameLabel.text = BY_USER.nickname;
    self.nameLabel = nameLabel;
    
    UILabel *noteLabel = [[UILabel alloc] init];
    [headView addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_left).offset(0);
        make.top.equalTo(nameLabel.mas_bottom).offset(5);
        make.right.equalTo(headView).offset(-10);
        make.height.mas_equalTo(@20);
    }];
    noteLabel.text = @"记账、记录资金的点点滴滴";
    noteLabel.textColor = [UIColor whiteColor];
    noteLabel.font = FONT(14);
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -STATUS_NAV, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_NAV - 49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = headView;
    [self.view addSubview:self.tableView];
   
}

- (void)tapAciton:(UITapGestureRecognizer *)tap{
    
    if (![self isLogin]) {
        return;
    }
    if (tap.view.tag == 100) {
        
        ByMyInformationVC *vc = [[ByMyInformationVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    
    
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  3;
    
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0){
        return 2;
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellIdentifier = @"UITableViewCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        UILabel *line = [[UILabel alloc] init];
        [cell addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell).offset(0);
            make.right.equalTo(cell).offset(0);
            make.bottom.equalTo(cell).offset(0);
            make.height.mas_offset(@1);
        }];
        line.backgroundColor = BY_LINE_COLOR;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIView *line = [[UIView alloc] init];
    [cell.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(0);
        make.right.equalTo(cell.contentView).offset(0);
        make.bottom.equalTo(cell.contentView).offset(0);
        make.height.mas_equalTo(@1);
    }];
    line.backgroundColor = BY_LINE_COLOR;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [cell.contentView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell.contentView).offset(0);
        make.left.equalTo(cell.contentView).offset(10);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    NSArray *ary = @[@[@"shehzi",@"yijianfankui"],@[@"tuijianhaoyou"],@[@"bangzhuzhongxin"]];
    imageView.image = [UIImage imageNamed:ary[indexPath.section][indexPath.row]];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(20);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.size.mas_equalTo(CGSizeMake(150, 20));
    }];
    
    titleLabel.textColor = TEXT_COLOR;
    titleLabel.font = FONT(16);
    
    NSArray *itemsAry = @[@[@"设置",@"意见反馈"],@[@"推荐给好友"],@[@"帮助中心"]];
    titleLabel.text = itemsAry[indexPath.section][indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


// 设置组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
     return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];// 取消选中阴影
    if (![self isLogin]) {
        return;
    }
    if (indexPath.section == 0){
        
        if (indexPath.row == 0) {
            
            ByMyInformationVC *vc = [[ByMyInformationVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            
        }else{
            //ByFeedbackVC *vc = [[ByFeedbackVC alloc] init];
            ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:@"http://web1.jinpu.8raw.com/suggest/add"];
            vc.titleString = @"意见反馈";
            [self by_normalPush:vc hasNavi:YES];
        }
        
    }else if (indexPath.section == 1){
        
        //[self.popView show];

    }else if (indexPath.section == 2){
        
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:@"http://web1.jinpu.8raw.com/help/index"];
        vc.titleString = @"帮助中心";
        [self by_normalPush:vc hasNavi:YES];
    }
    
}

- (BOOL)isLogin{
    ByModelUser *user = [ByCacheUser getUserInfo];
    if (user) {
        return YES;
    }else{
        ByLoginVC *vc = [[ByLoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
